<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - AI简历解析系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .register-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
            text-align: center;
        }

        .register-header {
            margin-bottom: 30px;
        }

        .register-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .register-header p {
            color: #666;
            font-size: 16px;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .register-button {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .register-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .register-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .login-link {
            margin-top: 20px;
            color: #667eea;
            text-decoration: none;
            font-size: 14px;
            display: inline-block;
        }

        .login-link:hover {
            text-decoration: underline;
        }

        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 10px;
            display: none;
        }

        .success-message {
            color: #27ae60;
            font-size: 14px;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-header">
            <h1>创建账户</h1>
            <p>加入AI智能简历解析系统</p>
        </div>

        <form id="registerForm" onsubmit="handleRegister(event)">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required minlength="3">
            </div>

            <div class="form-group">
                <label for="email">邮箱</label>
                <input type="email" id="email" name="email" placeholder="请输入邮箱" required>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required minlength="6">
            </div>

            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" placeholder="请再次输入密码" required>
            </div>

            <div class="error-message" id="errorMessage"></div>
            <div class="success-message" id="successMessage"></div>

            <button type="submit" class="register-button">注册</button>
        </form>

        <a href="/login" class="login-link">已有账户？立即登录</a>
    </div>

    <script>
        function handleRegister(event) {
            event.preventDefault();//阻止表单的默认提交行为（默认会跳转到 action指定的 URL）。这是为了让开发者通过 JavaScript 自定义提交逻辑（如异步请求）。
            
            const username = document.getElementById('username').value;
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const errorMessage = document.getElementById('errorMessage');
            const successMessage = document.getElementById('successMessage');

            // 重置消息
            errorMessage.style.display = 'none';
            successMessage.style.display = 'none';

            // 验证输入
            if (!email.includes('@')) {
                errorMessage.textContent = '请输入有效的邮箱地址';
                errorMessage.style.display = 'block';
                return;
            }

            if (password !== confirmPassword) {
                errorMessage.textContent = '两次输入的密码不一致';
                errorMessage.style.display = 'block';
                return;
            }

            if (password.length < 6) {
                errorMessage.textContent = '密码长度至少6位';
                errorMessage.style.display = 'block';
                return;
            }

            // 显示加载状态
            const registerBtn = document.querySelector('.register-button');
            registerBtn.disabled = true;
            registerBtn.innerHTML = '注册中...';

            // 调用后端注册接口
            fetch('/api/auth/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ username, email, password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.message === '注册成功，请登录') {
                    // 注册成功，跳转到登录页面
                    successMessage.textContent = '注册成功，请登录...';
                    successMessage.style.display = 'block';
                    
                    setTimeout(() => {
                        window.location.href = '/login';
                    }, 500);
                } else {
                    throw new Error(data.message || '注册失败');
                }
            })
            .catch(error => {
                errorMessage.textContent = error.message || '注册失败，请重试';
                errorMessage.style.display = 'block';
                registerBtn.disabled = false;
                registerBtn.innerHTML = '注册';
            });
        }

        // 检查是否已登录
        document.addEventListener('DOMContentLoaded', function() {
            if (localStorage.getItem('token')) {
                window.location.href = '/';
            }
        });

        // 回车键注册
        document.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                document.getElementById('registerForm').dispatchEvent(new Event('submit'));
            }
        });
    </script>
</body>
</html>